//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {
    loading:true,
    name:'',
    phone:'',
    email:'',
    id:''
  },

  // 页面第一次加载
  onLoad: function (options) {
    var that = this;

    app.getUserInfo(function (res) {
      console.log(res);
      that.setData({
        avatar: res.userInfo.avatarUrl,
        userName: res.userInfo.nickName
      })
    });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    })
   that.getData();
    
  },
  onShow:function(){
    this.getData();
  },
  getData:function(){
    var that = this;
    //普通用户个人接口
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'myDetail',
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          console.log(res);
          if (data.done && data.retval) {
            that.setData({
              count: data.retval.count,
              member: app.turn(data.retval.member),
              loading: false
            })
            console.log(that.data.member);
            if (that.data.member[0].is_doctor == 1) {
              wx.request({
                url: app.api,
                data: {
                  app: 'doctor',
                  act: "myDoctor",
                  t_session: session
                }, success: function (res) {
                  var data = res.data;
                  if (data.done && data.retval) {
                    that.setData({
                      doctor: data.retval.doctor,
                      consultArr: app.turn(data.retval.consultArr),
                      id: data.retval.doctor.id
                    })
                  }
                }
              })
            }
          }
        },
        fail: function (res) {
          that.setData({
            loading: true,
            errorTips: "app:menber连接失败"
          })
        }
      })
    })
  },
  // 个人资料
  show:function(){
    var that = this;
    that.setData({
      showdata:that.data.showdata==true?false:true
    })
  },
  //联系小编
  show_continue:function(){
    var that = this;
    that.setData({
      continue: that.data.continue == true ? false : true
    })
  }, 
  onShareAppMessage() {
    return this.data.shareInfo
  },
  //个人资料姓名
  name:function(res){
    var that = this;
    that.setData({
      name:res.detail.value
    })
  },
  //个人资料电话
  phone: function (res) {
    var that = this;
    that.setData({
      phone: res.detail.value
    })
  },
  //个人资料邮箱
  email: function (res) {
    var that = this;
    that.setData({
      email: res.detail.value
    })
  },
  submit:function(){
    var that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var reg = /^[\u4E00-\u9FA5]{2,4}$/;
    var myregemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    console.log(that.data.phone);
    if (that.data.name == "") {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (!reg.test(that.data.name)) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.phone == "") {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.phone.length != 11) {
      wx.showToast({
        title: '号码长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (!myreg.test(that.data.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    //对电子邮件的验证
    if (that.data.email == "") {
      wx.showToast({
        title: '请输入邮箱',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (!myregemail.test(that.data.email)) {
      wx.showToast({
        title: '邮箱有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }else{
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api,
          data: {
            app: 'member',
            act: 'modifyData',
            username: that.data.name,
            email: that.data.email,
            phone: that.data.phone,
            t_session: session
          },
          success:function(res){
            var data = res.data;
            if(data.done&&data.retval.code==1){
              wx.showToast({
                title: '修改成功',
                icon:'success'
              })
              that.setData({
                showdata: that.data.showdata == true ? false : true
              })
            }else{
              wx.showToast({
                title: '修改失败',
                icon:'loading'
              })
            }
          }
        })
      })
      
    }
  },
  //我的订单
  order:function(){
    wx.showModal({
      title: '温馨提示',
      content: '购物功能还在完善，暂不投入使用'
    })
  }

};

// 注册页面
Page(init);
